package com.demo.thymeleafdemo.servlet;

import com.demo.thymeleafdemo.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * @author ddone
 * @date 2024/3/13-10:51
 */
@WebServlet("/ognl")
public class OgnlServlet extends ViewBaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 添加user对象
        User user = new User(1001, "张三");
        req.setAttribute("user", user);
        // 2.添加userList
        ArrayList<User> userList = new ArrayList<>(Arrays.asList(new User(1002, "李四"), user));
        req.setAttribute("userList", userList);
        // 3. 添加Map集合
        HashMap<String, User> userMap = new HashMap<String, User>() {{
            put("user", user);
        }};
        req.setAttribute("userMap",userMap);
        // 4. if和unless判断
        req.setAttribute("flag",false);
        // 5. 测试switch
        req.setAttribute("level",2);

        this.processTemplate("ognl",req,resp);
    }
}
